package com.lizhi.yun.sync.rsz.handler;/**
 * @program: hbsf_yun
 * @name RszPrescriptionSyncHandler
 * @description: 处方信息
 * @author: lizhi
 * @create: 2021-11-10 14:13
 */

import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.rsz.dao.RszPrescriptionDao;
import com.lizhi.yun.db.rsz.dao.RszPrescriptionItemDao;
import com.lizhi.yun.db.rsz.entity.RszPrescriptionEntity;
import com.lizhi.yun.db.rsz.entity.RszPrescriptionItemEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@program: hbsf_yun
 *@name RszPrescriptionSyncHandler
 *@description: 处方信息
 *@author: lizhi
 *@create: 2021-11-10 14:13
 */
@Slf4j
@Service
public class RszPrescriptionSyncHandler {

    @Resource
    private RszPrescriptionDao rszPrescriptionDao;

    @Resource
    private RszPrescriptionItemDao rszPrescriptionItemDao;

    public String recv(String json){
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        try {
            RszPrescriptionEntity rszPrescriptionEntity = SamJsonUtil.toObject(json, RszPrescriptionEntity.class);
            log.info("康复磨砂卓处方信息上传:{}", rszPrescriptionEntity);
            RszPrescriptionEntity prescriptionEntity = rszPrescriptionDao.selectById(rszPrescriptionEntity.preId, rszPrescriptionEntity.hospitalId, rszPrescriptionEntity.hospitalSystemId);
            rszPrescriptionItemDao.deleteByPreId(rszPrescriptionEntity.preId, rszPrescriptionEntity.hospitalId, rszPrescriptionEntity.hospitalSystemId);
            List<RszPrescriptionItemEntity> rszPrescriptionItemEntities = rszPrescriptionEntity.rszPrescriptionItemEntitys;
            for (RszPrescriptionItemEntity rszPrescriptionItemEntity : rszPrescriptionItemEntities){
                rszPrescriptionItemEntity.lid = StringUtils.newId();
                rszPrescriptionItemEntity.dataUpState = rszPrescriptionEntity.dataUpState;
                rszPrescriptionItemEntity.dataUpTime = rszPrescriptionEntity.dataUpTime;
                rszPrescriptionItemEntity.hospitalId = rszPrescriptionEntity.hospitalId;
                rszPrescriptionItemEntity.hospitalSystemId = rszPrescriptionEntity.hospitalSystemId;
                rszPrescriptionItemDao.insertRecord(rszPrescriptionItemEntity);
            }
            if (!StringUtils.isEmpty(prescriptionEntity)){
                rszPrescriptionEntity.lid = prescriptionEntity.lid;
                rszPrescriptionDao.updateByIdSelective(rszPrescriptionEntity);
            } else {
                rszPrescriptionEntity.lid = StringUtils.newId();
                rszPrescriptionDao.insertRecord(rszPrescriptionEntity);
            }
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        } catch (Exception e){
            e.printStackTrace();
        }
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
